package org.springblade.core.tool.utils;

import java.util.ArrayList;
import java.util.List;

public class BaseRoleUtil {

    /**
     * 添加角色
     * @param roles
     * @param targetRole
     * @return
     */
    public static Long addRole(Long roles, Long targetRole) {
        //拥有目标角色
        if((roles & targetRole) != 0){
            return roles;
        }else{//添加角色
            roles =  roles | targetRole;
            return roles;
        }
    }

    /**
     * 移除角色
     * @param roles
     * @param targetRole
     * @return
     */
    public static Long removeRole(Long roles, Long targetRole) {
        //拥有目标角色
        if((roles & targetRole) != 0){
            //删除角色
            roles =  roles ^ targetRole;
            return roles;
        }else{
            return roles;
        }
    }


    /**
     * 是否拥有目标角色
     * @param roles
     * @param targetRole
     * @return
     */
    public static  boolean hasRole(Long roles, Long targetRole){
        return (roles & targetRole) != 0;
    }

    /**
     * 是否拥有目标列表中的角色
     * 注：只有一个角色符合条件即判断拥有
     * @param roles
     * @param listRole
     * @return
     */
    public static  boolean hasAnyRole(Long roles, List<Long> listRole){
        boolean result = false;
        for (int i = 0; i < listRole.size(); i++) {
			Long targetRole = listRole.get(i);
            result = hasRole(roles, targetRole);
            if(result){
                break;
            }
        }
        return result;
    }

    /**
     * 是否拥有目标列表中的角色
     * 注：全部拥有目标角色才返回true
     * @param roles
     * @param listRole
     * @return
     */
    public static  boolean hasAllRole(Long roles, List<Long> listRole){
        boolean result = true;
        for (int i = 0; i < listRole.size(); i++) {
			Long targetRole = listRole.get(i);
            result = hasRole(roles, targetRole);
            if(result){
                break;
            }
        }
        return result;
    }

    public static void main(String[] args) {
        //添加角色
		Long role = 1L;
		Long targetRole = 32L;
		Long result = addRole(role, targetRole);
        //Assert.isTrue(result.intValue() == 33);
		System.out.println(result==33);


        //移除角色
        role = 33L;
        targetRole = 32L;
        result = removeRole(role, targetRole);
        //Assert.isTrue(result==1);
		System.out.println(result==1);

        //查询是否拥有角色
        role = 33L;
        targetRole = 32L;
        boolean rb =hasRole(role, targetRole);
        //Assert.isTrue(rb);
		System.out.println(rb);

        //是否拥有列表中其中一条角色
        List<Long> list = new ArrayList<>();
        list.add(512L);
        list.add(128L);
        rb = hasAnyRole(role, list);
        //Assert.isFalse(rb);
		System.out.println(rb);

        list = new ArrayList<>();
        list.add(1L);
        list.add(128L);
        rb = hasAnyRole(role, list);
        //Assert.isTrue(rb);
		System.out.println(rb);

        //是否全部拥有列表中的角色
        list = new ArrayList<>();
        list.add(1L);
        list.add(32L);
        rb = hasAllRole(role, list);
        //Assert.isTrue(rb);
		System.out.println(rb);

        role = 128L;
        rb = hasAllRole(role, list);
        //Assert.isFalse(rb);
		System.out.println(rb);

    }
}
